﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using StockManagement.ViewModels;

namespace StockManagement.Views
{
    /// <summary>
    /// Interaction logic for MaterialsPage.xaml
    /// </summary>
    public partial class MaterialsPage : Frame
    {        
        NewMaterial _newMaterialFrame;
        InwardMaterial _editMaterialFrame;

        MaterialPageViewModel _materialPageVM;

        public MaterialsPage()
        {
            InitializeComponent();
            DataContextChanged += new DependencyPropertyChangedEventHandler(MaterialsPage_DataContextChanged);
            //rdAddProduct.IsChecked = true;
        }

        void MaterialsPage_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
                return;
            var matStockVM = e.NewValue as MaterialPageViewModel;
            if (matStockVM == null)
                return;
            SetMainGridDataContext(matStockVM);
        }

        private void SetMainGridDataContext(MaterialPageViewModel materialPageVM)
        {
            _materialPageVM = materialPageVM;
            _materialPageVM.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_materialPageVM_PropertyChanged);
            _mainGrid.DataContext = _materialPageVM;
            if (addeditFrame.Content == null)
                rdAddProduct.IsChecked = true;
        }

        void _materialPageVM_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            SetDataContextForAddEditFrame();
        }

        private void rdAddProduct_Checked(object sender, RoutedEventArgs e)
        {
            if (_materialPageVM == null)
                return;

            switch (rdAddProduct.IsChecked)
            {
                case true:
                    if (_newMaterialFrame == null)
                        _newMaterialFrame = new NewMaterial();

                   addeditFrame.Content = _newMaterialFrame;
                    break;
                case false:
                    if (_editMaterialFrame == null)
                        _editMaterialFrame = new InwardMaterial();

                    addeditFrame.Content = _editMaterialFrame;
                    break;
                default:
                    break;
            }

            SetDataContextForAddEditFrame();
        }

        private void SetDataContextForAddEditFrame()
        {
            switch (rdAddProduct.IsChecked)
            {
                case true:                                        
                    _newMaterialFrame.DataContext = _materialPageVM.NewMaterial;
                    break;
                case false:                    
                    _editMaterialFrame.DataContext = _materialPageVM.EditMaterial;
                    break;
                default:
                    break;
            }
            _dgMaterials.SetBinding(Frame.DataContextProperty, new Binding("MaterialGrid"));
        }
    }
}
